﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyBLL;
using MyDAL;
using MyEntity;

namespace MyUI
{
    public partial class FrmEdit : Form
    {


        public FrmEdit()
        {
            InitializeComponent();
        }
        private string StuID;
        public FrmEdit(string StuID)
        {
            this.StuID = StuID;
            InitializeComponent();
        }
        ProfessionInfoBLL professionInfoBLL = new ProfessionInfoBLL();
        StudentInfoBLL studentInfoBLL = new StudentInfoBLL();
        //绑定数据
        private void BindPro()
        {
            List<ProfessionInfoEntity> list = new List<ProfessionInfoEntity>();
            list = professionInfoBLL.List();
            list.Insert(0, new ProfessionInfoEntity { ProfessionID = 0, ProfessionName = "请选择" });
            this.cmbName.DataSource = list;
            //显示内容
            this.cmbName.DisplayMember = "ProfessionName";
            //编号
            this.cmbName.ValueMember = "ProfessionID";
        }

        private void BindData()
        {
            StudentInfoEntity stuentity = new StudentInfoEntity();
            stuentity = studentInfoBLL.Detail(Convert.ToInt32(StuID));
            this.textBox1.Text = stuentity.StuID;
            this.textBox2.Text = stuentity.StuName;
            this.textBox3.Text = stuentity.StuAge.ToString();
            if (stuentity.StuSex == "男")
            {
                this.checkBox1.Checked = true;
            }
            else
            {
                this.checkBox2.Checked = true;
            }
            string[] arr = stuentity.StuHobby.Split(',');
            foreach (string item in arr)
            {
                foreach (CheckBox item1 in panel2.Controls)
                {
                    if (item1.Text.Equals(item))
                    {
                        item1.Checked = true;
                    }
                }
            }

            this.cmbName.SelectedValue = stuentity.ProfessionID;

        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            BindPro();
            BindData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StudentInfoEntity stu = new StudentInfoEntity();
            stu.StuID = this.textBox1.Text;
            stu.StuName = this.textBox2.Text;
            stu.StuAge = Convert.ToInt32(this.textBox3.Text);
            if (this.checkBox1.Checked)
            {
                stu.StuSex = "男";
            }
            else
            {
                stu.StuSex = "女";
            }
            string hobby = "";
            foreach (CheckBox item in this.panel2.Controls)
            {
                if (item.Checked)
                {
                    if (!hobby.Equals(""))
                    {
                        hobby += ",";
                    }
                    hobby += item.Text;
                }
            }
            stu.StuHobby = hobby;
            stu.ProfessionID = Convert.ToInt32(this.cmbName.SelectedValue.ToString());
            int count = studentInfoBLL.Update(stu);
            if (count > 0)
            {
                MessageBox.Show("修改成功！");
                this.Close();
                //this.Owner.Close();
            }
            else
            {
                MessageBox.Show("修改失败！");
                this.Close();
            }
        }
    }
}
